package com.ymatou.userservice.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.userservice.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.common.enums.ChannelSourceEnum;
import com.ymttest.common.enums.UserTerminalSourceEnum;
import com.ymttest.database.model.YmtUsers;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 用户注册接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UserRegisterInfoRequestDto {

	private static UserRegisterInfoRequestDtoBean userBean;
	private static UserRegisterInfoRequestDtoCall userCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户注册接口");
	}

	@Before
	public void caseUp() {
		userBean = new UserRegisterInfoRequestDtoBean();
		userCall = new UserRegisterInfoRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(userCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_001() {
		Logger.start(true,"用户注册成功");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(6);
//			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
//			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);
			userBean.setEquimentid(TestDataManager.getRandomUUID().toString());
			userCall.setData(userBean);
			userCall.callService();
			Logger.verifyEquals(200, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("true", userCall.getString("Success"), "验证Success");
			verifyDB(userBean, userCall.getUserid());
			Map m=new HashMap<>();
			m.put("Uid", userCall.getUserid());
			Map history=new LoginHistoryWapper().selectUserRegHistoryByUid(m).get(0);
			Logger.debug(history);
			Logger.verifyEquals(userBean.getDeviceId(), history.get("DeviceId"), "验证凭据DeviceId");
			Logger.verifyEquals(userBean.getClientIp(), history.get("ClientIp"), "验证凭据ClientIp");
			Logger.verifyEquals("Alipay", history.get("ChannelSource"), "验证凭据ChannelSource");
			//1 App
			Logger.verifyEquals("MainSite", history.get("RegSource"), "验证凭据RegSource");
			Logger.verifyEquals(userBean.getEquimentid(), history.get("Equimentid"), "验证凭据CookieId");
			
			YmtUsers user = new YmtUsersWapper().getYmtUsers(userCall.getUserid());
			//验证cellphone
			Logger.verifyEquals(loginMobile, user.getScellphone(), "验证cellphone");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_002() {
		Logger.start(true,"app注册带deviceid用户注册成功");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(4);
			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 1;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;
			String deviceId=TestDataManager.getRandomUUID().toString();
			userBean.setDeviceId(deviceId);
			userBean.setLoginId(loginId);
			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);

			userCall.setData(userBean);
			userCall.callService();
			Logger.comment("result:" + userCall.getResult());
			Logger.comment("userid:" + userCall.getUserid());
			Logger.verifyEquals(200, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("true", userCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_003() {
		Logger.start(true,"logindid20位以上,用户注册成功");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(20);
//			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
//			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);

			userCall.setData(userBean);
			userCall.callService();
			Logger.verifyEquals(200, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("true", userCall.getString("Success"), "验证Success");
			verifyDB(userBean, userCall.getUserid());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_004() {
		Logger.start(false,"logindid100位以上,用户注册失败");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(100);
			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);

			userCall.setData(userBean);
			userCall.callService();
			Logger.verifyEquals(400, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("false", userCall.getString("Success"), "验证Success");
			String VerifyCode=userCall.getJsonObject().get("Result").getAsJsonObject().get("VerifyCode").getAsString();
			Logger.verifyEquals("LoginNameLengthError", VerifyCode, "验证返回的LoginNameLengthError信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_005() {
		Logger.start(true,"邮箱用户注册成功");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(4);
			String loginMail = loginId + "@ymatou.com";
//			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Other;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Pc;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
			userBean.setLoginMail(loginMail);
//			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);

			userCall.setData(userBean);
			userCall.callService();
			Logger.verifyEquals(200, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("true", userCall.getString("Success"), "验证Success");
			verifyDB(userBean, userCall.getUserid());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_006() {
		Logger.start(true,"传入TokenSource为sellerapp用户注册成功JIRACONN-2123");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(6);
//			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
//			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);
			userBean.setEquimentid(TestDataManager.getRandomUUID().toString());
			userBean.setTokenSource("sellerapp");
			userCall.setData(userBean);
			userCall.callService();
			Logger.verifyEquals(200, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("true", userCall.getString("Success"), "验证Success");
			verifyDB(userBean, userCall.getUserid());
			Map m=new HashMap<>();
			m.put("Uid", userCall.getUserid());
			Map history=new LoginHistoryWapper().selectUserRegHistoryByUid(m).get(0);
			Logger.debug(history);
			Logger.verifyEquals(userBean.getDeviceId(), history.get("DeviceId"), "验证凭据DeviceId");
			Logger.verifyEquals(userBean.getClientIp(), history.get("ClientIp"), "验证凭据ClientIp");
			Logger.verifyEquals("Alipay", history.get("ChannelSource"), "验证凭据ChannelSource");
			//1 App
			Logger.verifyEquals("MainSite", history.get("RegSource"), "验证凭据RegSource");
			Logger.verifyEquals(userBean.getEquimentid(), history.get("Equimentid"), "验证凭据CookieId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_007() {
		Logger.start(true,"传入TokenSource为app用户注册成功JIRACONN-2123");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(6);
//			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Other;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Ios;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
//			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);
			userBean.setEquimentid(TestDataManager.getRandomUUID().toString());
			userBean.setTokenSource("app");
			userBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			userCall.setData(userBean);
			userCall.callService();
			Logger.verifyEquals(200, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("true", userCall.getString("Success"), "验证Success");
			verifyDB(userBean, userCall.getUserid());
			Map m=new HashMap<>();
			m.put("Uid", userCall.getUserid());
			Map history=new LoginHistoryWapper().selectUserRegHistoryByUid(m).get(0);
			Logger.debug(history);
			Logger.verifyEquals(userBean.getDeviceId(), history.get("DeviceId"), "验证凭据DeviceId");
			Logger.verifyEquals(userBean.getClientIp(), history.get("ClientIp"), "验证凭据ClientIp");
			Logger.verifyEquals("Other", history.get("ChannelSource"), "验证凭据ChannelSource");
			//1 App
			Logger.verifyEquals("MainSite", history.get("RegSource"), "验证凭据RegSource");
			Logger.verifyEquals(userBean.getEquimentid(), history.get("Equimentid"), "验证凭据CookieId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_008() {
		Logger.start(false,"特殊字符用户注册失败");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(6)+"ā";
//			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
//			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);
			userBean.setEquimentid(TestDataManager.getRandomUUID().toString());
			userCall.setData(userBean);
			userCall.callService();
			Logger.verifyEquals(400, userCall.getResponseCode(), "验证code");
			Logger.verifyEquals("false", userCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserRegisterInfoRequestDto_009() {
		Logger.start(false,"中文字符用户注册成功");
		try {
			String loginId = "autotest_" + TestDataManager.randomStr(6)+TestDataManager.getChineseName();
//			String loginMail = loginId + "@ymatou.com";
			String loginMobile = TestDataManager.getPhoneNum();
			String pas = "123456";
			int registerType = 0;
			Boolean isAgreeContract = true;
			int regSource = 0;
			int visitLan = 0;
			ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
			UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
			String clientIp = "127.0.0.1";
			Long tokenTimeOut = (long) 1;

			userBean.setLoginId(loginId);
//			userBean.setLoginMail(loginMail);
			userBean.setLoginMobile(loginMobile);
			userBean.setPas(pas);
			userBean.setRegisterType(registerType);
			userBean.setIsAgreeContract(isAgreeContract);
			userBean.setRegSource(regSource);
			userBean.setVisitLan(visitLan);
			userBean.setChannelSource(channelSource);
			userBean.setTerminalSource(terminalSource);
			userBean.setClientIp(clientIp);
			userBean.setTokenTimeOut(tokenTimeOut);
			userBean.setEquimentid(TestDataManager.getRandomUUID().toString());
			userCall.setData(userBean);
			userCall.callService();
			verifyDB(userBean, userCall.getUserid());
			Map m=new HashMap<>();
			m.put("Uid", userCall.getUserid());
			Map history=new LoginHistoryWapper().selectUserRegHistoryByUid(m).get(0);
			Logger.debug(history);
			Logger.verifyEquals(userBean.getDeviceId(), history.get("DeviceId"), "验证凭据DeviceId");
			Logger.verifyEquals(userBean.getClientIp(), history.get("ClientIp"), "验证凭据ClientIp");
			Logger.verifyEquals("Alipay", history.get("ChannelSource"), "验证凭据ChannelSource");
			//1 App
			Logger.verifyEquals("MainSite", history.get("RegSource"), "验证凭据RegSource");
			Logger.verifyEquals(userBean.getEquimentid(), history.get("Equimentid"), "验证凭据CookieId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyDB(UserRegisterInfoRequestDtoBean userBean, int userid) throws Exception {
		ULC201406Bean ucb = new ULC201406Wapper().getULCInfoByUid(userid);
		Logger.verifyEquals(userBean.getLoginId().toLowerCase(), ucb.getlBills().getLoginId().toLowerCase(), "验证登陆ID");
		if(ucb.getlBills().getEmail()!=null){
			Logger.verifyEquals(userBean.getLoginMail().toLowerCase(), ucb.getlBills().getEmail().toLowerCase(), "验证邮箱");
		}
		Logger.verifyEquals(userBean.getLoginMobile(), ucb.getlBills().getMobile(), "验证手机");
		
	}
}